<?php
/*  
 * One Click Authentication
 * Copyright (C) 2008  Dream Production http://www.dreamproduction.ro
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


//Obtain the next two from Yahoo!
define('Y_APPID','YAHOO-APPID');
define('Y_SECRET','YAHOO-SECRET-KEY');

define('G_HTTP_CLIENT_ID','oka/1.0');	//set here the client id for makeing google requests
define('G_RETURN_URL','http://return/here/');	//set the return url for google authentication

$pathExtra = APP.DS.'vendors'.DS.PATH_SEPARATOR.VENDORS;
$path = ini_get('include_path');
$path = $pathExtra . PATH_SEPARATOR . $path;
ini_set('include_path', $path);

require_once 'Zend/Loader.php';
require_once 'YBrowserAuth.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_Query');

class AuthComponent extends Object
{
	var $controller = false;
	var $components = array('Session');

	var $yahooLoginURL = '';
	var $googleLoginURL = '';

    function getYahooLoginURL() {
	    return $this->yahooLoginURL;
    }
    
    function getGoogleLoginURL() {
    	return $this->googleLoginURL;
    }
      
	function startup(&$controller) {

		$this->controller = $controller;
		$ymail = new YahooMailJSONRPC(Y_APPID,Y_SECRET);

		$this->googleLoginURL = Zend_Gdata_AuthSub::getAuthSubTokenUri(G_RETURN_URL,
											  	'http://www.google.com/m8/feeds/groups', 0, 0);
		$this->yahooLoginURL = $ymail->getAuthURL();
		
		//here we set the links for Yahoo! BBAuth and Google AuthSub
		$controller->set('google_auth_url',$this->googleLoginURL);
		$controller->set('yahoo_auth_url',$this->yahooLoginURL);
		
		if (count($_GET) > 1 && isset($_GET['token'])) {
			//We have returned from authentication process
			$this->Session->write('Auth.token',$_GET['token']);
			if (isset($_GET['appid'])) {
				//Returned from Yahoo! Authentication
				$this->Session->write('Auth.provider','yahoo');
				//Check for valid authentication
				if ($ymail->validate_sig()) {
					$xml=$ymail->getAccessCredentials();
					if ($ymail == false) {
						$controller->set('auth_message',
							'WS CALL Failed. '. $ymail->sig_validation_error .
							'. '. $ymail->access_credentials_error);
						return;
					}
					$user = $ymail->GetUserData(new stdclass());
					// Error from JSON-RPC transport
					if ($user === false) {
						$controller->set('auth_message',
							'Error in JSON-RPC method: '.$ymail->web_services_error);
						return;
					}
					// Standard JSON-RPC error
					if (isset($user->error)) {
						$controller->set('auth_message',
							'Error from inside JSON-RPC data. Message: ' .
							$user->error->message .' Code: ' .
							$user->error->code);
						return;
					}
					$user = $user->result->data->userSendPref;
					$email = $user->defaultID;			//The email address
					$name = $user->defaultFromName;			//The fullname
				//Looks like this authentiction try is not valid.
				} else {
					$controller->set('auth_message',
						'BBauth authentication Failed. '. $ymail->sig_validation_error
					);
					return;
				}
			} else {
				//This looks like Google Authentication
				$this->Session->write('Auth.provider','google');
				$client = Zend_Gdata_AuthSub::getHttpClient($_GET['token']);
				$gdata = new Zend_Gdata($client,G_HTTP_CLIENT_ID);
				try {
					$feed = $gdata->getFeed(
						'http://www.google.com/m8/feeds/groups/default/base'
					);
				} catch (Exception $e) {
					$controller->set('auth_message',
						$e->getMessage() . ' <strong>' . $response->getMessage() . '</strong>');
					return;
				}
				$user = $feed->getAuthor();
				$name = $user[0]->getName();	//the full name
				$email = $user[0]->getEmail();	//the email
			}
			$this->Session->write('Auth.email',$email);
			$this->Session->write('Auth.name',$email);
		}
    }
	
	function logout() {
		$this->Session->delete('Auth');
	}
}
?>
